package com.example.content.views;

import android.content.Context;
import android.graphics.Color;

import androidx.core.content.ContextCompat;

import com.example.content.R;
import com.example.content.fragment.me.ProvinceInfo;
import com.google.gson.reflect.TypeToken;
import com.xuexiang.xui.widget.picker.widget.OptionsPickerView;
import com.xuexiang.xui.widget.picker.widget.builder.OptionsPickerBuilder;
import com.xuexiang.xui.widget.picker.widget.listener.OnOptionsSelectListener;
import com.xuexiang.xutil.net.JsonUtil;
import com.xuexiang.xutil.resource.ResourceUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhudongao
 * Desc
 * Date 2020/11/20
 */

public class WheelUtils {

    private List<ProvinceInfo> rawProvinceInfo = new ArrayList<>();
    private List<List<String>> citiesData = new ArrayList<>();

    private WheelUtils() {
        rawProvinceInfo.clear();
        citiesData.clear();
        rawProvinceInfo.addAll(getProvinceInfo());
        for (ProvinceInfo provinceInfo : rawProvinceInfo) {
            List<String> cityList = new ArrayList<>();
            for (ProvinceInfo.City city : provinceInfo.getCityList()) {
                cityList.add(city.getName());
            }
            citiesData.add(cityList);
        }
    }

    public static WheelUtils getInstance() {
        return InstanceHolder.wheelUtils;
    }


    private List<ProvinceInfo> getProvinceInfo() {
        return JsonUtil.fromJson(ResourceUtils.readStringFromAssert("province.json"),
                new TypeToken<List<ProvinceInfo>>() {
                }.getType());
    }

    public void showAddress(Context context, OnOptionsSelectListener listener) {
        OptionsPickerView addressView = new OptionsPickerBuilder(context, listener)
                .setTitleText("")
                .setDividerColor(Color.WHITE)
                .isRestoreItem(true)
                .setTextColorCenter(ContextCompat.getColor(context, R.color.textColorNormal))
                .setContentTextSize(20)
                .isDialog(false)
                .build();
        addressView.setPicker(rawProvinceInfo, citiesData);
        addressView.show();
    }

    public List<List<String>> getCitiesData() {
        return citiesData;
    }

    public List<ProvinceInfo> getRawProvinceInfo() {
        return rawProvinceInfo;
    }

    private static class InstanceHolder {
        private static WheelUtils wheelUtils = new WheelUtils();
    }
}
